package com.bupt.liutong.admin.sch.bo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.sch.dao.NewsDao;
import com.bupt.liutong.admin.sch.dto.News;
import com.bupt.liutong.admin.sch.form.NewsForm;
import com.bupt.liutong.util.BeanUtils;

public class NewsBo extends BaseBo {

	private NewsDao newsDao;

	public List<NewsForm> getNewsesByPage(NewsForm newsForm,
			HttpServletRequest request) {
		News news = new News();
		sm.pagingWithNoCriteria(newsForm, news, "sch_news.getNewsesCount");
		List<News> newses = newsDao.getNewsesByPage(news);
		return BeanUtils.dtoToForm(newses, NewsForm.class);
	}

	public NewsForm getNewsById(NewsForm newsForm) {
		NewsForm resultForm = new NewsForm();
		News news = newsDao.getNewsById(newsForm.getId());
		BeanUtils.copyProperties(resultForm, news);
		return resultForm;
	}

	@Transactional
	public void insertNews(NewsForm newsForm) {
		News news = new News();
		BeanUtils.copyProperties(news, newsForm);
		newsDao.insertNews(news);
	}

	@Transactional
	public void updateNews(NewsForm newsForm) {
		News news = new News();
		BeanUtils.copyProperties(news, newsForm);
		newsDao.updateNews(news);
	}
	
	@Transactional
	public void enableNews(String[] ids){
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				News news = new News();
				news.setId(Integer.valueOf(ids[i]));
				news.setEnabled(1);
				newsDao.updateNewsState(news);
			}
		}
	}
	
	@Transactional
	public void disableNews(String[] ids){
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				News news = new News();
				news.setId(Integer.valueOf(ids[i]));
				news.setEnabled(0);
				newsDao.updateNewsState(news);
			}
		}
	}

	@Transactional
	public void deleteNewses(String[] ids) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				newsDao.deleteNews(Integer.parseInt(ids[i]));
			}
		}
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}
}
